package com.demo.testui;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

/*
本页尝试对ActivityOriginal增加“通过一个值控制view的显示大小及点击范围”的功能，
很显然给出了一个失败的解决方法，不仅没有完成功能，原来能显示的3个view也不见了。

View     size:[0,0]
MyView     size:[0,0]
MyViewGroupsize:[0,0]
*/
public class ActivityProblem extends Activity {
    String TAG = Tags.problem;
    View mView;
    MyView mMyView;
    MyViewGroup mMyViewGroup;
    float percent =Constant.PERCENT_SHOW;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_view_size);

        //重置自定义的View
        MyView.percent = Constant.PERCENT_DEFAULT;
        MyViewGroup.percent = Constant.PERCENT_DEFAULT;

        init();
    }

    private void init() {
        mView = findViewById(R.id.click_area_view);
        mMyView = findViewById(R.id.click_area_myview);
        mMyViewGroup = findViewById(R.id.click_area_myviewgroup);
        mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(),"View",Toast.LENGTH_SHORT).show();
            }
        });
        mMyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(),"MyView",Toast.LENGTH_SHORT).show();
            }
        });
        mMyViewGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(),"MyViewGroup",Toast.LENGTH_SHORT).show();
            }
        });
        setClickSize(mView, percent, "  View     ");
        setClickSize(mMyView, percent, "MyView     ");
        setClickSize(mMyViewGroup, percent, "MyViewGroup");
    }

    private void setClickSize(View view, float percent, String tag) {
        Log.e(TAG, tag + "size:[" + view.getMeasuredWidth() + "," + view.getMeasuredHeight() + "]");
        ViewGroup.LayoutParams btnLayoutParams = (ViewGroup.LayoutParams) view.getLayoutParams();
        btnLayoutParams.width = (int) (view.getMeasuredWidth() * percent);
        btnLayoutParams.height = (int) (view.getMeasuredHeight() * percent);
        view.setLayoutParams(btnLayoutParams);
    }
}
